package com.cndongtai.util;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class MailUtil {
	
	public static void sendMail(String targetMail,String title, String content) throws UnsupportedEncodingException, MessagingException {
		Properties props = new Properties();
        props.setProperty("mail.transport.protocol", "smtps");
        props.setProperty("mail.smtps.host", "smtp.qiye.aliyun.com");
        props.setProperty("mail.smtps.auth", "true");
        props.setProperty("mail.smtps.socketFactory.port", "465");
        props.setProperty("mail.smtps.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.setProperty("mail.smtps.socketFactory.fallback", "false");
        props.setProperty("mail.smtps.starttls.enable", "true");

        Session session = Session.getDefaultInstance(props);
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress("dtdy666@cndongtai.com", "Torchn_Manager", "UTF-8"));
        message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(targetMail, title, "UTF-8"));
        message.setSubject(title, "UTF-8");
        message.setContent(content, "text/html;charset=UTF-8");
        message.setSentDate(new Date());
        Transport transport = session.getTransport("smtps");
        transport.connect("dtdy666@cndongtai.com", "Dongtai6");
        
        transport.sendMessage(message, message.getAllRecipients());
        
        transport.close();
	}
	
}
